﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using NLBS.Control;
using System.Text;
using System.IO;
using NLBS.Utility;
using NLBS.Data;
namespace NLBS.Admin.control
{
    public partial class database : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            checkAdmin();

            FileInfo file = new FileInfo(Request.PhysicalApplicationPath + "App_Data\\blog.mdb");
            StringBuilder sb = new StringBuilder();
            oldDataContent.Text = file.Length.ToString();
            if(Request.QueryString["action"]!=null)
            {
                string action=Request.QueryString["action"];
                string filePath=string.Empty;
                string path = Request.PhysicalApplicationPath + "App_Data\\";
                if (Request.QueryString["file"] != null)
                {
                    filePath =HttpUtility.UrlEncode(Request.QueryString["file"]);
                }
                switch (action)
                {
                    case "backup":
                        string newName = "blog_"+DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".bak";
                        SqlBackObject.Backup(file.FullName, path + newName);
                        break;
                    case "compact":
                        SqlBackObject.CompactAccess(file.FullName);
                        break;
                    case "restore":

                        SqlBackObject.RecoverAccess(path + filePath, file.FullName);
                        break;
                    case "delete":
                        File.Delete(path + filePath);
                        break;
                }
            }
            databaseRP.ItemDataBound += new RepeaterItemEventHandler(databaseRP_ItemDataBound);
            databaseRP.DataSource = FileHelp.GetFiles(Request.PhysicalApplicationPath + "App_Data\\");
            databaseRP.DataBind();
        }

        void databaseRP_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                FileInfo file = (FileInfo)e.Item.DataItem;
                
                if (file.Extension == ".bak")
                {
                    string realName = file.FullName.Replace(Request.PhysicalApplicationPath + "App_Data\\","");
                    Literal dataContent = (Literal)e.Item.FindControl("dataContent");
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("<a href=\"data/{0}\">", realName);
                    sb.Append("<img src=\"/styles/default/images/icon_file.gif\" />");
                    sb.AppendFormat("<b>{0}</b></a>", realName);
                    sb.AppendFormat(" | {0} | ", file.Length);
                    sb.AppendFormat("<a href=\"#\" onclick=\"AdminDataBase.restore('{0}')\">恢复</a> | ", realName);
                    sb.AppendFormat("<a href=\"#\" onclick=\"AdminDataBase.deleteData('{0}')\">删除</a> | ", realName);
                    sb.Append("<br />");
                    dataContent.Text = sb.ToString();
                }
            }
        }
    }
}